// miniprogram/pages/polularScience/doCheck/doCheck.js
const token = require('../../../serve/serve.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageCode:null,
    pageName:null,
    pageUrl:null,
    url_params:null,
    imageBase64:null,
    result: null,
    code:false,
    imageWidth: 0,//缩放后的宽
    imageHeight: 0,//缩放后的高
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id === "1") {
      this.setData({
        pageCode:1,
        pageName: "动物识别",
        pageUrl:'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' + token.access_token
      })
    }else if(options.id === "2") {
      this.setData({
        pageCode:2,
        pageName: "植物识别",
        pageUrl:'https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=' + token.access_token
      })
    }else if(options.id === "3") {
      this.setData({
        pageCode:3,
        pageName: "通用识别",
        pageUrl:'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=' + token.access_token
      })
    }else if(options.id === "4") {
      this.setData({
        pageCode:4,
        pageName: "车型识别",
        pageUrl:'https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token=' + token.access_token
      })
    }
    // this.getAccessToken();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.pageUrl);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(this.data.result)
  },
  /**
   * 上传图片方法
   */
  updataImage:function () {
    console.log(this);
    //wxAPI
    let _this = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '识别中，别着急行不',
        })
        const filePath = res.tempFilePaths[0]
        wx.getFileSystemManager().readFile({
          filePath: filePath,
          encoding: 'base64', 
          success: function(res){
            wx.request({
              method:'post',
              header:{
                'content-type':'application/x-www-form-urlencoded'
              },
              url: _this.data.pageUrl,
              data: {
                image: res.data,
                baike_num: 0
              },
              success:function(res){
                console.log(res.data.result);
                res.data.result.forEach(value => {
                  value.score = parseInt(value.score).toFixed(2);
                });
                _this.setData({
                  result:res.data.result,
                  code:true
                })
                console.log(_this.data.result)
              },
              complete: () => {
                wx.hideLoading()
              }
            })
          }
        })
        
      },
    })
  },
  // 获取accessToken
  getAccessToken:function () {
    wx.request({
      method:'post',
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      url: 'https://aip.baidubce.com/oauth/2.0/token',
      data: {
        grant_type:'client_credentials',
        client_id:'YOWIHhupFnqkxfXMBdjU604R',
        client_secret:'0LGz5ulhb0SASDi60ozNyrjADR3WudBq'
      },
      success:function(res){
        console.log(res);
      },
      complete: () => {
      }
    })
  },
  // 设置图片宽高
  imageLoad: function (e) {
    const img = e.detail
    this.setData({
      imageWidth: '650rpx',
      imageHeight: (Number(img.height)*650/Number(img.width))+'rpx'
    })
    console.log(this.data.imageWidth);
    console.log(this.data.imageHeight);
  },
  // 跳转百度
  goBaidu: function() {
    wx.navigateTo({
      url:'../out/out'
    })
  }
})